package com.isoft.demo01.controller.sys;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isoft.demo01.bean.BaseController;
import com.isoft.demo01.bean.ResponseData;
import com.isoft.demo01.bean.ResultCode;
import com.isoft.demo01.entity.Game;
import com.isoft.demo01.service.GameManageService;
import com.isoft.demo01.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/sys/game")
public class GameManageController extends BaseController {
    @Autowired
    GameManageService gameManageServiceSys;

    @Value("${sys.game.uppath}")
    private String gamePhotoPath;

    List<String>gamePhotoNames =new ArrayList<>();

    @PostMapping
    public ResponseData add(Game game){
        boolean r=false;
        if (null!=game){
            if (gamePhotoNames.size()>0){
                game.setPhoto(gamePhotoNames.toString().substring(1,gamePhotoNames.toString().length()-1));
                gamePhotoNames.clear();
            }
            r=gameManageServiceSys.add(game);
        }
        return getResponseData(r);
    }
    @PostMapping("img")
    public Map upGamePhoto(MultipartFile file , HttpServletRequest request) {
        String[] results = null ;
        if(null != file) {
            results = FileUtil.uploadFile(file, null, gamePhotoPath, request);
        }
        System.out.println(results[0] + "\n" + results[1]);
        Map<String  , Object> map = new HashMap<>() ;
        if(null != results) {
            gamePhotoNames.add(results[1]) ;
            Map<String , String> data = new HashMap<>() ;
            data.put("src" , results[0]) ;
            data.put("title" , "游戏图片") ;
            map.put("code" , 0) ;
            map.put("msg" , "上传成功") ;
            map.put("data" , data) ;
        } else {
            map.put("code" , 1) ;
            map.put("msg" , "上传失败") ;
            map.put("data" , null) ;
        }
        System.out.println(gamePhotoNames);
        return map ;
    }
    @DeleteMapping("{gameid}")
    public ResponseData delById(@PathVariable Integer gameid , HttpServletRequest request) {

        boolean r =gameManageServiceSys.delById(gameid , gamePhotoPath , request) ;
        return getResponseData(r) ;
    }

    @DeleteMapping
    public ResponseData delByIds(Integer[] gameid ,HttpServletRequest request) {
        boolean r = gameManageServiceSys.delByIds(gameid , gamePhotoPath , request) ;
        return getResponseData(r) ;
    }
    @GetMapping("{gameid}")
    public ResponseData getById(@PathVariable("gameid")Integer gameid){
        Game game = gameManageServiceSys.getById(gameid);
        ResponseData r= new ResponseData();
        r.setErrorCode(game!=null? ResultCode.SUCCESS.getCode() : ResultCode.COMMON_FAIL.getCode());
        r.setMsg(game != null ? ResultCode.SUCCESS.getMessage() : ResultCode.COMMON_FAIL.getMessage());
        r.setData(game);
        return r;
    }
    @GetMapping
    public Map<String ,Object>page(Integer curr,Integer limit,String gamename,Integer typeid,String introduce){
        Page<Game> pageInfo=gameManageServiceSys.getMore(curr,limit,gamename,introduce,typeid);
        Map<String , Object> map = new HashMap<>() ;
        if(null != pageInfo.getRecords() && pageInfo.getRecords().size() > 0) {
            map.put("code" , 0) ;
            map.put("msg" , "查找成功!") ;
        } else {
            map.put("code" , 1) ;
            map.put("msg" , "查无数据!") ;
        }
        map.put("count" , pageInfo.getTotal()) ;
        map.put("data" ,pageInfo.getRecords() ) ;
        return map ;
    }

    @PutMapping("{gameid}")
    public ResponseData update(@PathVariable("gameid")Integer gameid,Game game){
        game.setGameid(gameid);
        boolean r=gameManageServiceSys.update(game);
        return getResponseData(r);
    }
}
